package com.study.ssm.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * <pre>
 *     project name: SpringStudy
 *     author      : 李琼
 *     e-mail      : 1265137718@qq.com
 *     create time : 2017/9/15 上午2:12
 *     desc        : 描述--//MyBeanName TODO
 *     URL         : CSDN:http://blog.csdn.net/qq_22945151
 *                   GitHub:https://github.com/a1265137718
 *     Reference   ://
 *     modifier               :
 *     modification time      :
 *     modify remarks         :
 *
 *     @version: --//TODO
 * </pre>
 */


public class MyBeanName implements BeanNameAware ,ApplicationContextAware{
    private String name;

    /**
     * 这个例子的真正意义在于可以和ApplicationContextAware接口配合
     *
     * MyApplicationContext.setApplicationContext1766724936
     * MyBeanName.setBeanName: myBeanName
     * testGetMyBeanNamecom.study.ssm.aware.MyBeanName@33f88ab
     *
     * MyApplicationContext(setApplicationContext) -> MyBeanName(setBeanName)-> AwareTest(testGetMyBeanName )
     * @param name
     */
    @Override
    public void setBeanName(String name) {
        this.name = name;
        System.out.println("MyBeanName.setBeanName: "+name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyBeanName bean = (MyBeanName) applicationContext.getBean(name);
        System.out.println(bean.hashCode());
    }
}
